<?php
namespace app\api\controller\v1;
use app\api\logic\HuiLogic;
use app\service\UserWeixinService;
use think\Db;
use think\Log;
use think\Request;


class WxConnect extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }


    /**
    配置对接文件
     */
    public function checkSignature(Request $request)
    {
        log::write('微信对接消息');
        log::write($request->param());
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = 'moxiaomei';
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            log::write('正确');

            echo $_GET['echostr'];
        }else{
            log::write('错误');
            return false;
        }
    }


    public function make_mark(){
        $wxconfig = new WxConfig();
        $token = $wxconfig->get_token();
        dump($token);
        $send['action_name'] = 'QR_LIMIT_STR_SCENE';
        $send['action_info'] = ['scene' => ['scene_id'=>99999,'scene_str'=>'6205']];
        dump(json_encode($send,256));
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$token;
        $connect = new WxConnect();
        $result = $connect->request_post($url,json_encode($send,256));
        dump($result);die;

        //{"ticket":"gQGs8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyVzZrWDlaRHVlMkcxMDAwMHcwNzEAAgQ7LTZlAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02W6kX9ZDue2G10000w071"}
    }

    public function get_mark(){
        $ticket = 'gQGs8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyVzZrWDlaRHVlMkcxMDAwMHcwNzEAAgQ7LTZlAwQAAAAA';
        $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket;
        $info = HuiLogic::request_get($url);
        if (!empty($info)){
            $path = './lk/mark.png';
            $image = imagecreatefromstring($info);
            imagepng($image,$path);

        }

    }




}